package com.aspire.boc.mms.wmf.util;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.aspire.boc.mms.util.ResourceManager;
import com.huawei.insa2.comm.cmpp.message.CMPPMessage;
import com.huawei.insa2.comm.cmpp30.message.CMPP30DeliverMessage;
import com.huawei.insa2.util.Args;
import com.huawei.insa2.util.Cfg;
import com.huawei.smproxy.SMProxy30;

/**
 * @author shining
 * @version 2007-12-5 16:52:04
 */
public class MySMProxy30 extends SMProxy30 {
	private static Log logger = LogFactory.getLog(MySMProxy30.class);

	private static Log log = LogFactory.getLog("smslog");

	private static MySMProxy30 instance;
	private static Map<String, MySMProxy30> MySMProxy30Map = new HashMap<String, MySMProxy30>();

	public MySMProxy30(Args args) {
		super(args);
	}

	public synchronized static MySMProxy30 getInstance(String vasid) {
		MySMProxy30 instance = MySMProxy30Map.get(vasid);
		if (instance == null) {
			MySMProxy(vasid);
		}
		instance = MySMProxy30Map.get(vasid);
		return instance;
	}

	public synchronized static MySMProxy30 getInstance() {
		if (instance == null) {
			MySMProxy();
		}
		return instance;
	}

	private static void MySMProxy(String vasid) {
		try {
			Resource resource = new ClassPathResource("SMProxy" + vasid
					+ ".xml");
			String filepath = resource.getFile().getAbsolutePath();
			// String filepath = rm.getValue("sms.SMProxy.filepath");//配置文件路径
			logger.info("filepath---" + filepath);
			Args args = new Cfg(filepath, false).getArgs("CMPPConnect");
			logger.info("*********start login gateway**********");
			logger.debug(args.toString());
			MySMProxy30 instance = new MySMProxy30(args);
			MySMProxy30Map.put(vasid, instance);
			logger.info("*********login success**********");
		} catch (Exception e) {
			logger.info("getMySMProxy30  error:" + e);
		}

	}

	private static void MySMProxy() {
		try {
			ResourceManager rm = ResourceManager.getInstance();
			String defaultVasid = rm.getValue("sms.defaultVasid");
			instance = MySMProxy30Map.get(defaultVasid);
			if (instance == null) {
				Resource resource = new ClassPathResource("SMProxy.xml");
				String filepath = resource.getFile().getAbsolutePath();
				// String filepath =
				// rm.getValue("sms.SMProxy.filepath");//配置文件路径
				logger.info("filepath---" + filepath);
				Args args = new Cfg(filepath, false).getArgs("CMPPConnect");
				logger.info("*********start login gateway**********");
				logger.debug(args.toString());
				instance = new MySMProxy30(args);
				MySMProxy30Map.put(rm.getValue("sms.defaultVasid"), instance);
				logger.info("*********login success**********");
			}
		} catch (Exception e) {
			logger.info("getMySMProxy30  error:" + e);
		}

	}

	/**
	 * 对ISMG主动下发的消息的处理。
	 * 
	 * @param msg
	 *            收到的消息
	 * @return 返回的相应消息。
	 */
	public CMPPMessage onDeliver(CMPP30DeliverMessage msg) {
		/** @todo do some thing to handle this message. then return a response */
		if (msg.getRegisteredDeliver() == 0) {
			logger.info("*******收到mo*******");
			logger.info("phone:" + msg.getSrcterminalId() + " content:"
					+ new String(msg.getMsgContent()));
			log.info("phone:" + msg.getSrcterminalId() + " content:"
					+ new String(msg.getMsgContent()));
			logger.info(msg);
		} else {
			ByteBuffer buffer = ByteBuffer.wrap(msg.getStatusMsgId());
			long statusMsgId = buffer.getLong();
			logger.info("********收到状态报告********");
			logger.info("statMsgId" + statusMsgId + " stat:" + msg.getStat());
			log.info("statMsgId" + statusMsgId + " stat:" + msg.getStat());
			logger.info("phone:" + msg.getSrcterminalId());
		}
		return super.onDeliver(msg);
	}
}
